package com.tianyi.sfzy1.controller;

import com.tianyi.sfzy1.domain.Consultant;
import com.tianyi.sfzy1.domain.Consultationappointment;
import com.tianyi.sfzy1.service.ConsultantService;
import com.tianyi.sfzy1.util.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/consultant")
public class ConsultantController {
    @Autowired
    private ConsultantService consultantService;

    @GetMapping("/findAllConsultant")
    public R findAllConsultant(int pageNum, int pageSize) {
        return R.ok(consultantService.findAllConsultant(pageNum, pageSize));
    }
    @GetMapping("/findAllConsultant2")
    public R findConsultant2() {
        return R.ok(consultantService.findConsultant2());
    }

    @DeleteMapping("/deleteConsultant")
    public R deleteConsultant(int id) {
        return R.ok(consultantService.deleteConsultant(id));
    }
    @DeleteMapping("/deleteConsultantList")
    public R deleteByPrimaryKey(@RequestBody List<Integer> ids) {
        return R.ok(consultantService.deleteByPrimaryKey(ids));
    }

    @PostMapping
    public R insert(@RequestBody Consultant record) {
        return R.ok(consultantService.insert(record));
    }

    @PutMapping
    public R updateByPrimaryKeySelective(@RequestBody Consultant record) {
        return R.ok(consultantService.updateByPrimaryKeySelective(record));
    }

    @GetMapping("/findConsultantByCondition")
    public R findConsultantByCondition(@Param("realname") String realname,
                                       @Param("phone") String phone,
                                       @Param("gender") String gender,
                                       @Param("levelId") Integer levelId) {
        return R.ok(consultantService.findConsultantByCondition( realname, phone, gender, levelId));
    }

    //登录
    @GetMapping("/login")
    public R login(String username,String password){
        int i = consultantService.login(username,password);
        if (i == 1){
            return R.ok(UUID.randomUUID());
        }
        return R.fail("登录失败");
    }

    //根据用户名查询咨询师id
    @GetMapping("/findByUsername")
    public R findByUsername(String username){
        Consultant consultant = consultantService.findByUsername(username);
        if (consultant != null){
            return R.ok(consultant);
        }else {
            return R.fail("未找到该用户");
        }
    }
    @GetMapping("/findConsultantById")
    public R findConsultantById(Integer id){
        Consultant consultant = consultantService.findConsultantById(id);
        if (consultant != null){
            return R.ok(consultant);
        }else {
            return R.fail("未找到该用户");
        }
    }

    @GetMapping("/findConsultantByLevelAndSpecialization")
    public R findConsultantByLevelAndSpecialization(Integer level_id, Integer specialization_id){
        List<Consultant> consultants = consultantService.findConsultantByLevelAndSpecialization(level_id, specialization_id);
        if (consultants != null){
            return R.ok(consultants);
        }else {
            return R.fail("未找到该用户");
        }
    }

    @GetMapping("/findConsultantByFilters")
    public R findConsultantByFilters(@Param("area") String area,
                                     @Param("levelId") Integer levelId,
                                     @Param("gender") String gender,
                                     @Param("chargeTypeId") Integer chargeTypeId,
                                     @Param("tags") ArrayList<String> tags){
        return R.ok(consultantService.findConsultantByFilters(area, levelId, gender, chargeTypeId, tags));
    }

    @GetMapping("/findConsultantcertificateById")
    public R findConsultantcertificateById(Integer id){
        return R.ok(consultantService.findConsultantcertificateById(id));
    }

//    根据id查询咨询师
    @GetMapping("/findById")
    public R findById(int consultantId){
        Consultant consultant = consultantService.findById(consultantId);
        if (consultant!= null){
            return R.ok(consultant);
        }else {
            return R.fail("未找到该用户");
        }
    }

//    根据id修改咨询师信息
    @PutMapping("/updateConsultant")
    public R updateConsultant(int consultantId,String realname,String phone,int age,String gender){
        int i = consultantService.updateConsultant(consultantId,realname,phone,age,gender);
        if (i > 0){
            return R.ok();
        }
        return R.fail("修改失败");
    }

    //根据咨询师id修改咨询师头像
    @PutMapping("/updateConsultantAvatar")
    public R updateConsultantAvatar(int consultantId,String avatar){
        int i = consultantService.updateConsultantAvatar(consultantId,avatar);
        if (i > 0){
            return R.ok();
        }
        return R.fail("修改失败");
    }
}
